package com.ys.controller.api.sys;

import com.ys.config.MenuId;
import com.ys.entity.business.TpProductBq;
import com.ys.entity.sys.TsUserBankInfo;
import com.ys.entity.sys.YsCol;
import com.ys.enume.sys.YsColEnum;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.TsUserBankInfoService;
import com.ys.service.sys.UploadService;
import com.ys.util.ExportExcelUtils;
import com.ys.util.admin.ConditionUtil;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 员工银行账户管理
 */
@Controller
@RequestMapping("/api/inter/tsUserBankInfo")
public class ApiInterTsUserBankInfoController {
    private static final Logger logger = LoggerFactory.getLogger(ApiInterTsUserBankInfoController.class);

    @Autowired
    private CommonApiService<TsUserBankInfo> commonApiService;

    @Autowired
    private TsUserBankInfoService tsUserBankInfoService;

    @RequiresPermissions("tsUserBankInfo:view")
    @RequestMapping("/search")
    @ResponseBody
    public LayuiTableVoJA search(@ModelAttribute TsUserBankInfo cond,String conditions){
        //查询条件
        String nameLinked = ConditionUtil.getConditionValue(conditions, "_ys_nameLinked");//工号/姓名
        if (nameLinked != null){
            cond.where("B.UserId LIKE '%" + nameLinked + "%'" + " OR  B.UserName LIKE '%" + nameLinked + "%'");
        }
        cond.addLeftJoin("TS_UserInfo B on  B.UserId = A.UserId")
            .addField("B.deptName,B.roleName,(B.UserId + '/' + B.UserName) AS _ys_nameLinked")
        .setOrderby("A.createTime DESC");
        return commonApiService.searchLay(cond);
    }

    /**
     * 新增
     */
    @RequestMapping("/toAdd")
    @ResponseBody
    public R toAdd(HttpServletRequest request, @RequestParam Map<String, Object> params
    ) {
        try {
            logger.info("员工银行账户新增传参：" + params);
            //修改
            return tsUserBankInfoService.add(params);
        } catch (Exception e) {
            logger.error("员工银行账户新增出错：", e.getMessage(), e);
            return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
        }
    }


    /**
     * 修改
     */
    @RequestMapping("/toEdit")
    @ResponseBody
    public R toEdit(HttpServletRequest request, @RequestParam Map<String, Object> params
    ) {
        try {
            logger.info("员工银行账户修改传参：" + params);
            //修改
            return tsUserBankInfoService.edit(params);
        } catch (Exception e) {
            logger.error("员工银行账户修改出错：", e.getMessage(), e);
            return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
        }
    }

    @RequestMapping("/export")
    @ResponseBody
    public R export(@ModelAttribute TsUserBankInfo cond, String conditions) throws Exception{

        cond.setPage(0).setSize(999999);
        cond.addLeftJoin("TS_UserInfo B on  B.UserId = A.UserId")
        .addField("B.deptName,B.roleName,(B.UserId + '/' + B.UserName) AS _ys_nameLinked")
        .setOrderby("A.createTime DESC");

        List<Map<String, Object>> listData = cond.queryMap();


        List<YsCol> listField = new YsCol()
                .setMenuId(MenuId.TS_UserBankInfo)
                .setFrontShow(YsColEnum.FrontShow.SHOW.value)
                .setOrderby("A.SortNo asc")
                .queryList();

        ExportExcelUtils<TsUserBankInfo> util = new ExportExcelUtils<TsUserBankInfo>(new TsUserBankInfo());

        String toFilePath = util.exportExcel(listData, listField, "员工银行账户表");
        UploadService.UploadDto dto = commonApiService.uploadLocalToServer(toFilePath);
        return R.ok(dto.getUrl());
    }

    @RequestMapping("/searchGzFk")
    @ResponseBody
    public LayuiTableVoJA searchGzFk(@ModelAttribute TsUserBankInfo cond,String conditions){
        //查询条件
        String nameLinked = ConditionUtil.getConditionValue(conditions, "_ys_nameLinked");//工号/姓名
        if (nameLinked != null){
            cond.where("B.UserId LIKE '%" + nameLinked + "%'" + " OR  B.UserName LIKE '%" + nameLinked + "%'");
        }
        cond.addLeftJoin("TS_UserInfo B on  B.UserId = A.UserId")
                .addField("B.deptName,B.roleName,(B.UserId + '/' + B.UserName) AS _ys_nameLinked")
                .setOrderby("A.createTime DESC");
        cond.where("A.status  = 1");

        return commonApiService.searchLay(cond);
    }

}
